// table operations:

unsigned symbols_are_equal (int* le, int* ri) {
   if (le[0] != ri[0])
      return 0;
   for (int a = 1; a <= le[0]; a++) {
      if (le[a] != ri[a])
         return 0;
   }
   return 1;
}

Table* table_lookup (Table* tab, int* key) {
   while (tab) {
      if (symbols_are_equal(tab->key, key))
         return tab;
      tab = tab->next;
   }
   return NULL;
}

Table* table_set (Table** tab, int* key, Atom* value) {
   Table *nt = table_lookup(*tab, key);
   if (!nt) {
      nt = (Table*)ALLOC(sizeof(Table));
      nt->key = duplicate_symbol(key);
      nt->next = *tab;
      *tab = nt;
   }
   nt->value = duplicate_atom(value);
   return nt;
}

void table_union (Table* start, Table** finish) {
   if (!start || !finish)
      _error("TABLE_UNION: Empty table");
   Table *start_p = start;
   while (start->next) {
      start = start->next;
   }
   start->next = *finish;
   *finish = start_p;
}

unsigned table_unset (Table** tab, int* key) {
   Table *prev = NULL;
   Table *nav = *tab;
   while (nav) {
      if (symbols_are_equal(nav->key, key)) {
         if (prev == NULL) {
            *tab = nav->next;
         } else {
            prev->next = nav->next;
         }
         nav->next = NULL;
         free_table(nav);
         return 1;
      }
      prev = nav;
      nav = nav->next;
   }
   return 0;
}

int table_size (Table* tab) {
   int size = 0;
   while (tab) {
      size ++;
      tab = tab->next;
   }
   return size;
}
